/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visao;

import java.util.logging.Level;
import java.util.logging.Logger;
import negocio.GerenciarDB;

/**
 *
 * @author Leandro
 */
public class JanelaDeletarEst extends javax.swing.JFrame {

    /**
     * Creates new form DeletarEstabelecimento
     */
    public JanelaDeletarEst() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ConfirmarDelLoja = new javax.swing.JButton();
        CancelarDelLoja = new javax.swing.JButton();
        DelCNPJ = new javax.swing.JLabel();
        DeletarEstabelecimento = new javax.swing.JLabel();
        deletarLoja = new javax.swing.JFormattedTextField();
        todosCampos = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Deletar Estabelecimento");
        setMinimumSize(new java.awt.Dimension(670, 450));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        ConfirmarDelLoja.setText("Confirmar");
        ConfirmarDelLoja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConfirmarDelLojaActionPerformed(evt);
            }
        });
        getContentPane().add(ConfirmarDelLoja, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 270, 100, 30));

        CancelarDelLoja.setText("Cancelar");
        CancelarDelLoja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarDelLojaActionPerformed(evt);
            }
        });
        getContentPane().add(CancelarDelLoja, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 270, 100, 30));

        DelCNPJ.setText("Digite o CNPJ do Estabelecimento:");
        getContentPane().add(DelCNPJ, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 150, -1, 30));

        DeletarEstabelecimento.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        DeletarEstabelecimento.setText("Deletar Estabelecimento");
        getContentPane().add(DeletarEstabelecimento, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 30, -1, -1));

        try{  
            javax.swing.text.MaskFormatter data= new javax.swing.text.MaskFormatter("##.###.###/####-##");  
            deletarLoja = new javax.swing.JFormattedTextField(data);  
        }  
        catch (Exception e){  
        }
        deletarLoja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deletarLojaActionPerformed(evt);
            }
        });
        getContentPane().add(deletarLoja, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 150, 157, 30));

        todosCampos.setForeground(java.awt.Color.red);
        getContentPane().add(todosCampos, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 150, 270, 30));

        setSize(new java.awt.Dimension(728, 514));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void ConfirmarDelLojaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConfirmarDelLojaActionPerformed
        // TODO add your handling code here:
        String CNPJ = deletarLoja.getText().replaceAll("[.]","");
        CNPJ = CNPJ.replaceAll("[/]","");
        CNPJ = CNPJ.replaceAll("[-]","");
        
        if ((CNPJ.isEmpty()) || ("        ".equals(CNPJ)) ) {
            todosCampos.setText("O campo deve ser preenchido");
        } else {
            try {
                 GerenciarDB banco = new GerenciarDB();
                 
                 if (banco.checkEstabelecimentoDB(CNPJ)) {
                    banco.delEstabelecimento(CNPJ);
                    JanelaInicioAdmin frame = new JanelaInicioAdmin();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                    this.dispose();
                 } else {
                     todosCampos.setText("Estabelecimento incorreto ou inexistente.");
                 }
            } catch (Exception ex) {
                Logger.getLogger(JanelaDeletarCartao.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        
        
    }//GEN-LAST:event_ConfirmarDelLojaActionPerformed

    private void CancelarDelLojaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarDelLojaActionPerformed
        // TODO add your handling code here:
        this.dispose();
        JanelaInicioAdmin frame = new JanelaInicioAdmin();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }//GEN-LAST:event_CancelarDelLojaActionPerformed

    private void deletarLojaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deletarLojaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_deletarLojaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JanelaDeletarEst.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JanelaDeletarEst.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JanelaDeletarEst.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JanelaDeletarEst.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JanelaDeletarEst().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelarDelLoja;
    private javax.swing.JButton ConfirmarDelLoja;
    private javax.swing.JLabel DelCNPJ;
    private javax.swing.JLabel DeletarEstabelecimento;
    private javax.swing.JFormattedTextField deletarLoja;
    private javax.swing.JLabel todosCampos;
    // End of variables declaration//GEN-END:variables
}
